<template>
  <div class="app-page">
    <div style="margin-bottom: 10px;" class="btn-group">
      <div>
        <el-button v-permission="1" type="primary" icon="el-icon-search" @click="search">查询</el-button>
        <el-button v-permission="2" type="primary" icon="el-icon-plus" @click="add">新增</el-button>
      </div>
    </div>
    <lb-table
      ref="lbTable"
      v-el-height-adaptive-table="{bottomOffset:10}"
      border
      highlight-current-row
      element-loading-text="加载中"
      :column="tableData.column"
      :data="tableData.data"
      pagination
      :total="tableData.total"
      custom-column
      @fetch="fetchData"
    />
    <add ref="add" />
  </div>
</template>

<script>
  import { queryWechatConfigList, wechatDelete } from '@/api'
  import Add from './components/Add'

  export default {
    name: 'wechatConfig',
    components: {
      Add
    },
    data() {
      return {
        tableData: {
          total: 0,
          column: [
            {
              prop: 'accountDesc',
              label: '公众账号描述',
              width: 150
            },
            {
              prop: 'accountAppId',
              label: '公众账号appId',
              width: 150
            },
            {
              prop: 'accountAppSecret',
              label: '公众账号appSecret',
              width: 300
            },
            {
              label: '操作',
              align: 'center',
              fixed: 'right',
              width: 150,
              render: (h, { row, $index }) => {
                return (
                  <div>
                    <el-link v-permission='3' size='mini' onClick={() => this.update(row, $index)}>
                      编辑
                    </el-link>
                    <el-link v-permission='4' size='mini' type='danger' onClick={() => this.del(row, $index)}>
                      作废
                    </el-link>
                  </div>
                )
              }
            }
          ],
          data: []
        }
      }
    },
    methods: {
      _getQuery(obj) {
        const query = {}
        for (const key in obj) {
          if (obj.hasOwnProperty(key)) {
            if (obj[key] !== '') {
              query[key] = obj[key]
            }
          }
        }
        return query
      },
      async fetchData({ currentPage, pageSize }) {
        const query = {
          currentPage,
          pageSize
        }
        const res = await queryWechatConfigList(query)
        this.tableData.data = res.data.rows
        if (currentPage === 1) this.tableData.total = res.data.total
      },
      async add(row) {
        await this.$refs.add.open(row)
        this.$refs.lbTable.fetch({ currentPage: 1 })
      },
      async update(row) {
        await this.$refs.add.open(row)
        this.$refs.lbTable.fetch({ currentPage: 1 })
      },
      del(row, index) {
        this.$confirm('确定要作废该公众号？', '提示', {
          type: 'warning'
        }).then(() => {
          const loading = this.$message({
            message: '正在删除',
            duration: 0,
            iconClass: 'el-icon-loading'
          })
          wechatDelete({ id: row.id }).then(() => {
            this.tableData.data.splice(index, 1)
            this.$notify({
              title: '提示',
              message: '删除成功',
              type: 'success',
              duration: 2000
            })
          }).finally(() => {
            loading.close()
          })
        })
      },
      search() {
        this.$refs.lbTable.fetch({ currentPage: 1 })
      }
    }
  }
</script>

<style lang="scss" scoped>
.el-form-item .el-form-item {
  margin-bottom: 0;
}
.merge-item {
  /deep/ .el-form-item__content {
    display: flex;
  }
}
.btn-group {
  display: flex;
  justify-content: space-between;
}
</style>
